package org.phoenixframework.api.rbac.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.phoenixframework.common.security.config.TokenManager;
import org.phoenixframework.common.tools.Constants;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelife
 * Date: 2020/8/14 23:50
 * description:
 */
public class SecurityUtils {

    public static String getUsername() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    public static Long getUserId() {
        // 这里取得是token
        String token = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
        Claims claims = Jwts.parser().setSigningKey(Constants.JWT_SIGNING_KEY).parseClaimsJws(token).getBody();
        return claims.get(Constants.USER_ID, Long.class);
    }

}
